<?php
namespace Home\Controller;
use Think\Controller;
use Home\Model\SqliteStainfo;
use Home\Model\SqliteStainfo2;
class ApwifidataController extends BaseController {

    public function  getdata(){
        if(IS_POST){
            $sqlitestainfo=new SqliteStainfo();
            $stadata= $sqlitestainfo->order(array( 'time'=>'desc'))->select();

            $stainfodatable= D('Stainfo');
            $stainfodatable->where(1)->delete();

            $count=count($stadata);
            if($stadata){
                for($iloop=0; $iloop < $count; $iloop++){
                    $vendor = parent::$macvendor[substr($stadata[$iloop]['s_mac'], 0, 8)];
                    if($vendor == ""){
                        $vendor="未知厂家";
                    }
                    $map[$iloop]= array(
                        'id'=>$iloop+1,
                        'requsetssid'=>base64_encode( $stadata[$iloop]['essid']),
                        'smac'=>$stadata[$iloop]['s_mac'],
                        'vendor'=>$vendor,
                        'imac'=>$stadata[$iloop]['b_mac'],
                        'dmac'=>$stadata[$iloop]['d_mac'],
                        'dimac'=>$stadata[$iloop]['b_mac'].'<BR>'.$stadata[$iloop]['d_mac'],
                        'time'=>$stadata[$iloop]['time'],
                        'encryption'=>$stadata[$iloop]['en_mode'],
                        'signalstrength'=>abs(intval($stadata[$iloop]['signal']) ),
                        'devname'=>$stadata[$iloop]['device_name'],
                        'distance'=>'['.round(pow(10,abs(intval($stadata[$iloop]['signal']))
                                    /(10*4))*10)/100 . '米]',
                        'timeparse'=>substr($stadata[$iloop]['time'],0,4).'/'.substr($stadata[$iloop]['time'],4,2).'/'
                            .substr($stadata[$iloop]['time'],6,2).' '
                            .substr($stadata[$iloop]['time'],8,2).':'
                            .substr($stadata[$iloop]['time'],10,2).':'
                            .substr($stadata[$iloop]['time'],12,2).'',
                        'gps'=>$stadata[$iloop]['position']
                    );
                }
            }
            if(C('TWOSCANCARD')=='true'){
                $sqlitestainfo2=new SqliteStainfo2();
                $stadata= $sqlitestainfo2->order(array( 'time'=>'desc'))->select();
                $count2=count($stadata);
                if($stadata){
                    for($iloop=0; $iloop < $count2; $iloop++){
                        $map[$count+$iloop]= array(
                            'id'=>$iloop+1,
                            'requsetssid'=>base64_encode( $stadata[$iloop]['essid']),
                            'smac'=>$stadata[$iloop]['s_mac'],
                            'imac'=>$stadata[$iloop]['b_mac'],
                            'dmac'=>$stadata[$iloop]['d_mac'],
                            'dimac'=>$stadata[$iloop]['b_mac'].'<BR>'.$stadata[$iloop]['d_mac'],
                            'time'=>$stadata[$iloop]['time'],
                            'encryption'=>$stadata[$iloop]['en_mode'],
                            'signalstrength'=>abs(intval($stadata[$iloop]['signal']) ),
                            'devname'=>$stadata[$iloop]['device_name'],
                            'distance'=>'['.round(pow(10,abs(intval($stadata[$iloop]['signal']))
                                        /(10*4))*10)/100 . '米]',
                            'timeparse'=>substr($stadata[$iloop]['time'],0,4).'/'.substr($stadata[$iloop]['time'],4,2).'/'
                                .substr($stadata[$iloop]['time'],6,2).' '
                                .substr($stadata[$iloop]['time'],8,2).':'
                                .substr($stadata[$iloop]['time'],10,2).':'
                                .substr($stadata[$iloop]['time'],12,2).'',
                            'gps'=>$stadata[$iloop]['position']
                        );
                    }
                }
                //对数据进行排序
                array_multisort(array_column($map,'time'),SORT_DESC,$map);
                for($loop=0;$loop<count($map);$loop++){
                    $map[$loop]['id']=$loop+1;
                }
            }
            /*if($count){
                $stainfodatable->addAll($map);
            }


            $stainfodata = $stainfodatable->order(array( 'time'=>'desc'))->select();
            */
            if($map==null){
                $map=[];
            }
            $this->ajaxReturn(array('data'=>$map), 'JSON');
        }
    }



    public function getgpsbymac(){
        if(IS_POST){
            $sqlitestainfo=new SqliteStainfo();
            $stadata= $sqlitestainfo->where(array('s_mac="'.I('post.mac').'"') )->order(array( 'time'=>'desc'))->limit(1)->select();

            $this->ajaxReturn(array('data'=>$stadata), 'JSON');
        }
    }

    public function apwifidata(){
       /* $sqlitestainfo=new SqliteStainfo();
        $stadata= $sqlitestainfo->select();

        $stainfodatable= D('Stainfo');
        $stainfodatable->where(1)->delete();
        $count=count($stadata);
        if($stadata){
            for($iloop=0; $iloop < $count; $iloop++){
                $map[$iloop]= array(
                    'requsetssid'=>$stadata[$iloop]['essid'],
                    'smac'=>$stadata[$iloop]['s_mac'],
                    'imac'=>$stadata[$iloop]['b_mac'],
                    'dmac'=>$stadata[$iloop]['d_mac'],
                    'time'=>$stadata[$iloop]['time'],
                    'encryption'=>$stadata[$iloop]['en_mode'],
                    'signalstrength'=>$stadata[$iloop]['signal'],
                    'devname'=>$stadata[$iloop]['device_name'],
                );
            }
        }
        if($count){
            $stainfodatable->addAll($map);
        }


        $stainfodata = $stainfodatable->order(array( 'time'=>'desc'))->select();
        if($stainfodata==true){
            $this->assign("scanterminaldata",$stainfodata);
        }
    */

        $this->assign('title','终端检索');
        $this->assign('currentname','apwifidata');


        $this->display();
        return;

        //获取热点和终端列表
        $url=$_SESSION['cardinfo'][self::$cardtype_scan]['cardip'] . C('LUCI_WIFISCAN_URL') . 'TerminalDevInfo/nil%20nil%20nil%20nil%20nil%20';
        //var_dump($url);

        $retteerminaldata = http_post_json($url, null);
        //$retteerminaldata = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($retteerminaldata));
        $jsterminaldata = json_decode($retteerminaldata,TRUE);

        if($jsterminaldata){
            for($iloop=0; $iloop < count($jsterminaldata[0]); $iloop++){
                $map= array(
                    'requsetssid'=>$jsterminaldata[0][$iloop]['requsetSSID'],
                    'smac'=>$jsterminaldata[0][$iloop]['smac'],
                    'imac'=>$jsterminaldata[0][$iloop]['imac'],
                    'dmac'=>$jsterminaldata[0][$iloop]['dmac'],
                    'time'=>$jsterminaldata[0][$iloop]['time'],
                    'encryption'=>$jsterminaldata[0][$iloop]['encryption'],
                    'signalstrength'=>$jsterminaldata[0][$iloop]['signalStrength'],
                    'devname'=>$jsterminaldata[0][$iloop]['devName'],
                );
                //先查询是否存在相同ssid名称，相同mac
                if(D('Stainfo')->getSimCount( $jsterminaldata[0][$iloop]['smac'])<=0){
                    D('Stainfo')->add($map);
                }
                else{
                    D('Stainfo')->where(array('smac'=>$map['smac']))->save($map);
                }
            }
        }

        $stainfodata = D('Stainfo')->order(array( 'time'=>'desc'))->select();
        if($stainfodata==true){
            $this->assign("scanterminaldata",$stainfodata);
        }

        


        $this->display();
    }
}